package com.xp.tinyurl.admin.controller;

import com.xp.tinyurl.admin.auth.JwtUtil;
import com.xp.tinyurl.admin.auth.UserThreadLocal;
import com.xp.tinyurl.common.base.ResultVO;
import com.xp.tinyurl.common.base.ResultVOBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 登陆管理
 * @Author chengguo.pei
 * @Date 2021/9/22 18:22
 */
@Slf4j
@RequestMapping("/admin/login")
@RestController
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserThreadLocal userThreadLocal;

    /**
     * 获取当前用户
     * @return
     */
    @RequestMapping("currentUser")
    public ResultVO currentUser(){

        return ResultVOBuilder.success(userThreadLocal.get());

    }

    /**
     * 验证sso登陆（非页面接口）
     * @throws Exception
     */
    @RequestMapping("login")
    public ResultVO login() throws Exception{
        String name = "admin";
        //create token
        Map<String, String> claims = new HashMap<>();
        claims.put("name", name);
        String token = jwtUtil.buildToken(claims);
        return ResultVOBuilder.success(token);
    }

    /**
     * 退出登陆
     * @return
     * @throws Exception
     */
    @RequestMapping("logout")
    public ResultVO logoutBySSO() throws Exception{
        //TODO set token expire

        return ResultVOBuilder.unAuthErr("系统退出","");
    }


}
